# coding:utf-8
__author__ = 'han'

#图书馆功能：查看，删除，借出，归还。最下面有例子及方法如何使用。
class Library(object):
    def __init__(self,bookdb,borrowdb):
        self.book=bookdb
        self.borrowdb=borrowdb

#显示现有的所有图书
    def bookList(self):
        booklist=file(self.book,'rb').readline().strip().split(' ')
        bookset=set(booklist)
        file(self.book,'rb').close()
        borrowlist=file(self.borrowdb,'rb').readline().strip().split(' ')
        borrowset=set(borrowlist)
        count=0
        borrowcount=0
        #显示图书馆现在有多少本书
        #print borrowlist
        for item in bookset:
            if item=='':
                return 'Library None'
            count +=1
            print '%s << %s >> Num : %s \n' %(count,item,booklist.count(item))
            #print '\n'
        #显示借出了什么书本
        for borrow in borrowset:
            # print 'at here < %s >' %borrow,type(borrow)
            #判断如果借书目录里面是空，就返回没有书借出，如果不是则返回下面的信息
            if borrow=='':
                return 'No book borrow!'
            borrowcount+=1
            print 'borrow book %s << %s >> Num : %s' %(borrowcount,borrow,borrowlist.count(borrow))

    #查看图书数量：
    def bookdbcount(self,name):
        bookdb=file(self.book,'rb')
        bookname=bookdb.readline().strip().split()
        return bookname.count(name)

    #查看借出数量
    def borrowcount(self,name):
        borrowdb=file(self.borrowdb,'rb')
        borrowname=borrowdb.readline().strip().split()
        return borrowname.count(name)

    #增加图书
    def addBook(self,content,number=1):
        while number!=0:
            bookwrite=file(self.book,'ab')
            bookwrite.write(' ')
            bookwrite.write(content)
            bookwrite.close()
            number-=1
    #删除图书   #如何在本类中调取自己的函数
    def delBook(self,d,number=1):
        #print Library.bookdbcount(d)
        if self.bookdbcount(d)<number:
            return 'number exceed'
        while number!=0:
            bookdel=file(self.book,'rb')
            delbooklist=bookdel.readline()
            if delbooklist=='':
                return 'Can not delete books already!\n'

            delsplit=delbooklist.split()
            if d not in delsplit:
                return "we don't have this book,please check you input!\n"

            delsplit.remove(d)
            book=file(self.book,'wb')
            book.write('')
            book.close()
            bookdel.close()
            for i in delsplit:
                bookrevert=file(self.book,'ab')
                bookrevert.write(' ')
                bookrevert.write(i)
                bookrevert.close()
            number-=1

    #借书  #增加还书数量，更改print 为return
    def borrowBook(self,borrow):
        #读文件模式打开存书文件
        opensavefiler=file(self.book,'rb')
        #读存书文件
        readsavefile=opensavefiler.readline()
        if readsavefile=='':
            return 'sorry We have no can take the books out!\n'
        #整读文件，去掉开头结尾
        savebooksplit=readsavefile.split()
        #删除借出的书
        if borrow not in savebooksplit:
            return "we don't have this book!\n"
        savebooksplit.remove(borrow)
        #写文件模式打开存书文件
        opensavefilew=file(self.book,'wb')
        #清空文件
        opensavefilew.write('')
        #关闭写文件文件
        opensavefilew.close()
        #追加模式打开借书文件
        openborrowa=file(self.borrowdb,'ab')
        #添加空格
        openborrowa.write(' ')
        #插入借的书
        openborrowa.write(borrow)
        openborrowa.close()
        for i in savebooksplit:
            bookrevert=file(self.book,'ab')
            bookrevert.write(' ')
            bookrevert.write(i)
            bookrevert.close()

    #还书    现在是说 openborrowfile 里面是空值！！！
    def returnBook(self,borrow):
        #读文件模式打开借书文件
        openborrowfiler=file(self.borrowdb,'rb')
        #读借书文件
        openborrowfile=openborrowfiler.readline()
        if openborrowfile=='':
            return 'This book is not our,please check you input book name!\n'
        #整借书文件，去掉开头结尾
        borrowbooksplit=openborrowfile.split()
        #删除借书记录
        if borrow not in borrowbooksplit:
            return 'This book is not our,please check you input book name!\n'
        borrowbooksplit.remove(borrow)
        #写文件模式打开借书文件
        openborrowfilew=file(self.borrowdb,'wb')
        #清空文件
        openborrowfilew.write('')
        #关闭写文件文件
        openborrowfilew.close()
        #追加模式打开书库文件
        opensavefilea=file(self.book,'ab')
        #添加空格
        opensavefilea.write(' ')
        #插入借的书
        opensavefilea.write(borrow)
        opensavefilea.close()
        for i in borrowbooksplit:
            bookrevert=file(self.borrowdb,'ab')
            bookrevert.write(' ')
            bookrevert.write(i)

#Library.bookList('./book','./borrowdb')
a=Library('./book','./borrowdb')
#a.addBook('qilongzhu')
#print a.delBook('qilongzhu')
a.borrowBook('jiqimao')
#a.returnBook('qilongzhu')
print a.bookList()
#print a.bookdbcount('jiqimao')
#print a.borrowcount('qilongzhu')